const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
Page({
  data: {
    store: [],
    all: []
  },
  onLoad() {
    var openId = wx.getStorageSync('openId');
    db.collection('ztd').where({
        _openid: openId
      })
      .get()
      .then(res => {
        setTimeout(() => {
          this.setData({
            store: res.data
          })
        }, 200)
      });
     var store = wx.getStorageSync('store');
      if(store.length == 0){
    db.collection('ztd').where({
        _openid: _.exists(false)
      })
      .get()
      .then(res => {
        wx.setStorageSync('store', res.data);           /*放在缓存*/
      })
      .catch(err => {})
    }
  },
  selectList(event) {
    /*更新其他自提点页面*/
    var openId = wx.getStorageSync('openId');
    const index = event.currentTarget.dataset.index;
    let store = this.data.store;
    /*写入数据库*/
    let p1 = new Promise((resolve, reject) => {
      db.collection('ztd').where({
          _openid: openId
        })
        .get()
        .then(res => {
          resolve(res.data);
        })
    })
    Promise.all([p1]).then((result) => {
      let myzt = result[0];
      let cho = store[index];
      let name = cho.name;
      let detail = cho.detail;
      let image = cho.image;
      let num = cho.num;
      if (myzt.length > 1) {
        wx.showToast({
          title: '只显示二个我的！',
          duration: 2000,
          icon: 'error'
        })
      } else {
        if (myzt.length == 0) {
          db.collection('ztd').add({
            data: {
              name: name,
              detail: detail,
              image: image,
              num: num,
              selected: true,
              del:false
            }
          })
        } else {
          if (num in myzt) {   
           
          } else {
            db.collection('ztd').add({
              data: {
                name: name,
                detail: detail,
                image: image,
                num: num,
                selected: true,
                del:false
              }
            })
          }
        }
      }
    })
      /*更新另一页面*/
      let cho = store[index];
      let arr = [];
      arr.push(cho);
      let id = arr[0]._id;
      let pages = getCurrentPages(); //获取当前页面
      let beforePage = pages[pages.length - 2]; //获取上一个页面的实例  
      //修改上一个页面的响应式数据
      beforePage.setData({
        store: arr
      })
      wx.showToast({
        title: '地址更换成功',
        duration: 1000,
        icon: 'success'
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)
    
  },
  previewImg: function (e) {
    var store = this.data.store;
    let arr = [];
    for (let i = 0; i < store.length; i++) {
      arr.push(store[i].image)
    }
    wx.previewImage({
      current: e.currentTarget.dataset.imgurl, //当前图片地址
      urls: arr
    })
  },
  qt: function (e) {
    /*其他自提点包括已选的*/
    let store = wx.getStorageSync('store');
    this.setData({
      store: store
    })
  },
  me: function (e) {
    var openId = wx.getStorageSync('openId');
    db.collection('ztd').where({
        _openid: openId
      })
      .get()
      .then(res => {
        setTimeout(() => {
          this.setData({
            store: res.data
          })
        }, 200)
      });
  }
})